<?php
declare(strict_types=1);
namespace Service\ConfigureService;
use Service\BaseService;
use App\Model\BbcModel\ConfigChannel;
use Service\Code\Normal;

/**
 * 渠道配置查询服务
 * @author chenshubo
 */
class ChannelConfigService extends BaseService
{
    /**
     * 获取渠道配置
     * @param $param
     */
    public function getConfig( array $param = [])
    {
        if (empty($param['code'])) {
            return Normal::returnCode(Normal::PARAM_BUS_ERROR,'expect param code');
        }
        if (empty($param['cid'])) {
            return Normal::returnCode(Normal::PARAM_BUS_ERROR,'expect param cid');
        }
        $data = ConfigChannel::instance()->info(['code' => $param['code'], 'cid' => $param['cid'], 'status' => 1]);
        if (empty($data)) {
            return Normal::returnCode(Normal::PARAM_BUS_ERROR,'config not exist');
        }
        $data['value'] = !empty($data['value']) ? json_decode($data['value'], true) : $data['value'];
        return Normal::returnCode(Normal::SUC,'获取成功',$data);
    }
    
    /**
     * @新渠道预写入配置
     * @param    array $param
     * @access   public
     * @return   array
     */
    public function initConfig(array $param = [])
    {
        if (empty($param['cid'])) {
            return [
                'status' => Normal::PARAM_BUS_ERROR,
                'msg'    => 'expect param cid',
            ];
        }
    
        $time = time();
        foreach ($this->init_config as $item)
        {
            $config = ConfigChannel::instance()->info(['cid' => $param['cid'], 'code' => $item['code']]);
            if (!empty($config)) {
                continue;
            }
            $add_data = [
                'cid'  => $param['cid'],
                'name' => $item['name'],
                'type' => $item['type'],
                'code' => $item['code'],
            ];
            $this->add($add_data);
        }
    
        return [
            'status' => Normal::SUC,
            'msg'    => '写入成功',
        ];
    }
    
    /**
     * 获取共享配置
     * @param array $param
     * @return string[]|unknown[]|number[]
     */
    public function getShare( array $param )
    {
        $b2b=$this->getConfig(['cid'=>$param['cid'],'code'=>'b2b_share']);
        $b2b_share=$b2b['data']['value']??[];
        $share['b_store_title']=$b2b_share['title']??'';
        $share['b_cover']=$b2b_share['img']??'';
        return [
            'status' => Normal::SUC,
            'msg' => '获取成功',
            'data' => $share,
        ];
    }
    
    /**
     * 获取协议服务
     * @param $param
     * @return string[]|number[]|string[][][]
     */
    public function getAgreement( array $param)
    {
        $return=[
            'userAgreement'=>[
                'title'=>'用户服务协议',
                'content'=>''
            ],
            'privacyAgreement'=>[
                'title'=>'隐私权政策',
                'content'=>''
            ],
        ];
        //隐私协议
        $data = ConfigChannel::instance()->info(['code' => 'privacy_agreement', 'cid' => $param['cid'], 'status' => 1]);
        if(!empty($data)){
            $data['value'] = !empty(json_decode($data['value'])) ? json_decode($data['value'], true) : $data['value'];
            if($data['value']){
                $return['privacyAgreement']['content']=$data['value']['privacy_agreement_content'];
            }
        }
        //用户协议
        $data2 = ConfigChannel::instance()->info(['code' => 'user_agreement', 'cid' => $param['cid'], 'status' => 1]);
        if(!empty($data2)){
            $data2['value'] = !empty(json_decode($data2['value'])) ? json_decode($data2['value'], true) : $data2['value'];
            if($data2['value']){
                $return['userAgreement']['content']=$data2['value']['user_agreement_content'];
            }
        }
        return [
            'status' => Normal::SUC,
            'msg' => '获取成功',
            'data' => $return,
        ];
    }
    
    /**
     * 获取开店图片
     * @param array $param
     * @return array
     */
    public function getImage(array $param=[])
    {
        if($param['mnid']==2){
            $image = ConfigChannel::instance()->info(['code' => 'wx_mnapp', 'cid' => $param['cid'], 'status' => 1]);
        }else{
            $image = ConfigChannel::instance()->info(['code' => 'wx_q_mnapp', 'cid' => $param['cid'], 'status' => 1]);
        }
        if(!$image){
            return [
                'status' => Normal::SUC,
                'msg'    => '请求成功',
                'data' => 'https://indoorbuydev.oss-cn-hangzhou.aliyuncs.com/new/upload/20211229/164076210090407.png',
            ];
        }
        $image = json_decode($image['value'],true)['img'];
        if(!$image){
            return [
                'status' => Normal::SUC,
                'msg'    => '请求成功',
                'data' => 'https://indoorbuydev.oss-cn-hangzhou.aliyuncs.com/new/upload/20211229/164076210090407.png',
            ];
        }
        return [
            'status' => Normal::SUC,
            'msg'    => '请求成功',
            'data'   => $image
        ];
    }
    
    /**
     * 获取渠道配置
     * @param string $code
     * @param int $cid
     */
    public function getChannelConfig( string $code ,int $cid)
    {
        $data = ConfigChannel::instance()->info(['code' => $code, 'cid' => $cid, 'status' => 1]);
        return $data['value']?@json_decode($data['value'], true):[];
    }
    
    
    public function getPriceSet(array $param=[])
    {
        $data = ConfigChannel::instance()->info(['code' => 'price_decimal', 'cid' => $param['cid'], 'status' => 1]);
        if(empty($data)){
            return 0;
        }
        if(empty($data['value'])){
            return 0;
        }else{
            $value = json_decode($data['value'], true);
            return $value['price'];
        }
    }

    /**
     * 获取支付手续费
     * @param
     */
    public function getPayFee( string $fee)
    {
        if( $fee > 0 && $fee < 1000 ) {
            $pay_fee = bcsub('1',bcdiv((string)$fee, '1000', 3),3);
        }else{
            $pay_fee = 0.994;
        }
        return $pay_fee;
    }
    
    public function openStoreConfig($param)
    {
        $info = OpenStoreConfig::instance()->info(['cid'=>$param['cid']]);
        if(!$info){ //未配置开店页面
            $info['is_pay'] =  0;
            $info['audit_type'] = 1;
            $info['open_store_page'] = 1;
            $info['open_store_page_type'] = 1;
            $info['file_url'] = 'https://indoorbuydev.oss-cn-hangzhou.aliyuncs.com/new/upload/20230131/167515537188018.png';
            $info['settlement_agreement'] = file_get_contents('https://indoorbuydev.oss-cn-hangzhou.aliyuncs.com/new/upload/20230131/167515593579321.txt');
            return ['status'=>Normal::SUC,'msg'=>'默认配置','data'=>$info];
            $wx_mnapp = $this->info(['code' => 'wx_mnapp', 'cid' => $param['cid'], 'status' => 1]);
            if(!$wx_mnapp){
                $image = 'https://indoorbuydev.oss-cn-hangzhou.aliyuncs.com/new/upload/20211229/164076210090407.png';
            }else{
                $image = json_decode($wx_mnapp['value']) ?? '';
            }
            return ['status'=>Normal::SUC,'msg'=>'请求成功','data'=>['file_url'=>$image]];
        }
        $returnData['is_video'] = 0;
        if($info['open_store_page'] == 0 && $info['file_url']){
            $returnData['is_video'] = 1;
        }
        $returnData['is_pay'] = $info['is_pay'];
        $returnData['money'] = $info['money'];
        $returnData['settlement_agreement'] = $info['settlement_agreement'];
        $returnData['open_store_page_type'] = $info['open_store_page'];
        $returnData['store_image'] = explode(',',$info['store_image']);
        $returnData['file_url'] = $info['file_url'];
        $returnData['store_name'] = $info['store_name'];
        if($info['open_store_page'] == 0){ //自定义配置
            $goods_lists = [];
            $level = 0;
            if($param['mid']){
                $store = Mnuser::instance()->info(['mid'=>$param['mid'],'pid'=>$param['cid'],'new_store'=>1]);
                if($store){
                    $level = $store['level'];
                }
            }
            $price_type = 0;
            $permit = ConfigChannel::instance()->info(['cid'=>$param['cid'],'code'=>'btb_buy']);
            if(!empty($permit) && !empty($permit['value'])){
                $value = json_decode($permit['value'],true);
                if(isset($value['price_type']) && $value['price_type']==1){
                    $price_type = 1;
                }
            }
            $showTag = GoodsTags::instance()->lists(['cid'=>$param['cid'],'show'=>1,'_fields_'=>'id,show_name,img,position']);
            $settingPrice = ConfigChannel::instance()->getPriceSet(['cid'=>$param['cid']]);
            $goods_id_arr = explode(',',$info['goods_ids']);
            foreach ($goods_id_arr as $v){
                $info = Goods::instance()->formatChannelGoodsList(['goods_id'=>$v,'cid'=>$param['cid'],'level'=>$level,'price_type'=>$price_type,'mid'=>$param['mid'],'showTag'=>$showTag,'settingPrice'=>$settingPrice]);
                if($info){
                    $goods = Goods::instance()->info(['id'=>$info['id'],'_fields_'=>'price_recommend,price_store']);
                    $distribution_price = bcsub($goods['price_recommend'],$goods['price_store'],2);
                    if($distribution_price == 0){
                        $distribution_price = 0;
                    }
                    $info['distribution_price'] = $distribution_price;
                    $goods_lists[] = $info;
                }
            }
            $returnData['goods_list'] = $goods_lists;
        }
        if(!$returnData['file_url']){
            $returnData['file_url'] = 'https://indoorbuydev.oss-cn-hangzhou.aliyuncs.com/new/upload/20211229/164076210090407.png';
        }
        return ['status'=>Normal::SUC,'msg'=>'请求成功','data'=>$returnData];
    }
}
